
from domination import core, run
import shutil
from tempfile import mkstemp
from shutil import move
from os import remove, close

def replace(file, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    new_file = open(abs_path,'w')
    old_file = open(file)
    for line in old_file:
        new_file.write(line.replace(pattern, subst))
    #close temp file
    new_file.close()
    close(fh)
    old_file.close()
    #Remove original file
    remove(file)
    #Move new file
    move(abs_path, file)


class MyScenario(run.Scenario):
    REPEATS = 10
    SETTINGS = core.Settings(max_steps=600, think_time=100)
    #SETTINGS = core.Settings(max_steps=600, think_time=0.01)
# The class name of each agentdistributeAmmo
agent1 = "AgentBase"
agent2 = "AgentBase"

# Copy main file
mainFile = "domination/main.py"
mainFileCopy1 = "domination/mainDONOTEDIT1_" + agent1 + ".py"
mainFileCopy2 = "domination/mainDONOTEDIT2_" + agent2 + ".py"
shutil.copyfile(mainFile, mainFileCopy1)
shutil.copyfile(mainFile, mainFileCopy2)

# Open files and edit to use the right class
#f1 = open(mainFileCopy1)
#f2 = open(mainFileCopy2)
replace(mainFileCopy1, "__AUTOREPLACEME__", agent1);
replace(mainFileCopy2, "__AUTOREPLACEME__", agent2);


MyScenario.test(red="domination/crippledAgent.py", blue=mainFileCopy2)
#MyScenario.test(red=mainFileCopy1, blue="domination/crippledAgent.py")


#MyScenario.one_on_one(red=mainFileCopy1, blue=mainFileCopy2)
